/************************************************************************************/
/*  Program name CAPFNDIMP_CONC_PROG_FLEX_TL.sql                                    */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_CONC_PROG_FLEX_TL';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_CONC_PROG_FLEX_TL;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	--  RENAME UNIQUE KEY ???
	UPDATE
		FND_DESCRIPTIVE_FLEXS_TL
	SET
		Descriptive_Flexfield_Name	=	NVL(CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,7,4),Descriptive_Flexfield_Name),
										CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,11,4),Descriptive_Flexfield_Name)),
		Title						=	NVL(CAPBRIMP_MAPPING.RenameStr(SUBSTR(Title,7,4),Title),
										CAPBRIMP_MAPPING.RenameStr(SUBSTR(Title,11,4),Title))
	WHERE	(Application_ID,Descriptive_Flexfield_Name,Language) IN
		(
			SELECT
				Application_ID,Descriptive_Flexfield_Name,Language
			FROM
				CRST_FND_CONC_PROG_FLEX_TL
		);
		
	UPDATE
		FND_DESCRIPTIVE_FLEXS_TL
	SET
		Descriptive_Flexfield_Name	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Descriptive_Flexfield_Name,11,4),Descriptive_Flexfield_Name),
		Title						=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Title,11,4),Title)
	WHERE	(Application_ID,Descriptive_Flexfield_Name,Language) IN
		(
			SELECT
				Application_ID,Descriptive_Flexfield_Name,Language
			FROM
				CRST_FND_CONC_PROG_FLEX_TL
		);

	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO FND_DESCRIPTIVE_FLEXS_TL
			VALUES
			(
			C1.APPLICATION_ID,
			C1.DESCRIPTIVE_FLEXFIELD_NAME,
			C1.LANGUAGE,
			C1.TITLE,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),		--LAST_UPDATED_BY,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),			--CREATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.FORM_CONTEXT_PROMPT,
			C1.SOURCE_LANG,
			C1.DESCRIPTION
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
